CLASS zcl_feishu_job_001 DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    DATA gv_token TYPE string.
    INTERFACES if_apj_dt_exec_object .
    INTERFACES if_apj_rt_exec_object .

  PROTECTED SECTION.
  PRIVATE SECTION.

    CONSTANTS app_secret TYPE string VALUE 'rpSQut0hifypNSLGvWqxJdZKM64VD2C4'.
    CONSTANTS app_id TYPE string VALUE 'cli_a7bb77ce43f8900c'.

    " Approval Code
    CONSTANTS _p1 TYPE string VALUE '差旅费用报销' ##NO_TEXT.
    CONSTANTS approval_code_01 TYPE string VALUE '138BA444-D18C-4580-B04B-315D0868F270' ##NO_TEXT.

    CONSTANTS _p2 TYPE string VALUE '招待费用报销' ##NO_TEXT.
    CONSTANTS approval_code_02 TYPE string VALUE 'EC524513-17FC-47EF-8F7C-0650112EF646' ##NO_TEXT.

    CONSTANTS _p3 TYPE string VALUE '日常费用报销' ##NO_TEXT.
    CONSTANTS approval_code_03 TYPE string VALUE '11E10B80-9D1A-470D-82C5-E9943A7469C0' ##NO_TEXT.

    CONSTANTS _p4 TYPE string VALUE '付款申请' ##NO_TEXT.
    CONSTANTS approval_code_04 TYPE string VALUE '5B927BD0-4F81-4554-B412-E40D0B1707C8' ##NO_TEXT.

    CONSTANTS _p5 TYPE string VALUE '员工借支申请' ##NO_TEXT.
    CONSTANTS approval_code_05 TYPE string VALUE '390BD819-F38C-4C1F-9655-560AF4AE0C8B' ##NO_TEXT.

    CONSTANTS _p6 TYPE string VALUE '特殊付款申请' ##NO_TEXT.
    CONSTANTS approval_code_06 TYPE string VALUE 'D44F05F7-FF84-472A-B3E5-6EACBE97748F' ##NO_TEXT.
ENDCLASS.



CLASS ZCL_FEISHU_JOB_001 IMPLEMENTATION.


  METHOD if_apj_dt_exec_object~get_parameters.
    " the supported selection parameters
    et_parameter_def = VALUE #(
   (  selname = 'P_MOD1'  kind = 'P' radio_group_ind = 'X' datatype = 'C' length = 1 param_text = _p1  changeable_ind  = 'X' radio_group_id = 'G1' )
   (  selname = 'P_MOD2'  kind = if_apj_dt_exec_object=>parameter radio_group_ind = 'X' datatype = 'C' length = 1 param_text = _p2 changeable_ind  = 'X' radio_group_id = 'G1' )
   (  selname = 'P_MOD3'  kind = if_apj_dt_exec_object=>parameter radio_group_ind = 'X' datatype = 'C' length = 1 param_text = _p3 changeable_ind  = 'X' radio_group_id = 'G1' )
   (  selname = 'P_MOD4'  kind = if_apj_dt_exec_object=>parameter radio_group_ind = 'X' datatype = 'C' length = 1 param_text = _p4 changeable_ind  = 'X' radio_group_id = 'G1' )
   (  selname = 'P_MOD5'  kind = if_apj_dt_exec_object=>parameter radio_group_ind = 'X' datatype = 'C' length = 1 param_text = _p5 changeable_ind  = 'X' radio_group_id = 'G1' )
   (  selname = 'P_MOD6'  kind = if_apj_dt_exec_object=>parameter radio_group_ind = 'X' datatype = 'C' length = 1 param_text = _p5 changeable_ind  = 'X' radio_group_id = 'G1' )
     ).

    " Default paramrters values
    et_parameter_val = VALUE #(
     (  selname = 'P_MOD1'  kind = if_apj_dt_exec_object=>parameter  sign = 'I' option = 'EQ' low = 'X' )
    ).
  ENDMETHOD.


  METHOD if_apj_rt_exec_object~execute.
    DATA: p_mod1,p_mod2,p_mod3,p_mod4,p_mod5,p_mod6  TYPE  abap_bool.
    DATA: ls_appid TYPE t_appid.
    DATA: fs_interface TYPE REF TO lcl_fs_interface.

    LOOP AT it_parameters INTO DATA(ls_paramenters).
      CASE ls_paramenters-selname.
        WHEN 'P_MOD1' .
          p_mod1 = ls_paramenters-low.
        WHEN 'P_MOD2' .
          p_mod2 = ls_paramenters-low.
        WHEN 'P_MOD3' .
          p_mod3 = ls_paramenters-low.
        WHEN 'P_MOD4' .
          p_mod4 = ls_paramenters-low.
        WHEN 'P_MOD5' .
          p_mod5 = ls_paramenters-low.
        WHEN 'P_MOD6' .
          p_mod6 = ls_paramenters-low.
      ENDCASE.
    ENDLOOP.

    CLEAR ls_appid.
    ls_appid-app_id =  app_id.
    ls_appid-app_secret = app_secret.

    fs_interface = NEW #( ).

    " 1、 获取token
    gv_token  =  fs_interface->get_token( ls_appid ).
    IF gv_token IS NOT INITIAL.
      "      DATA:gt_ins TYPE TABLE of t_ins.

      " 2、 获取INSTANCES
      CASE abap_true.
        WHEN p_mod1.
*          DATA(ls_taks) =  fs_interface->get_tasks( iv_token = gv_token  iv_approval_code = approval_code_01 ).
          DATA(lt_taks) =  fs_interface->get_tasks( iv_token = gv_token  iv_approval_code = approval_code_01 ).
          " TODO 3、获取实例信息
          LOOP AT lt_taks ASSIGNING FIELD-SYMBOL(<fs>).
            DATA(lt_data1) = fs_interface->get_instance( iv_token = gv_token iv_instance = <fs>-code ).
            CLEAR lt_data1.
          ENDLOOP.
          " TODO 4、EML 凭证录入

          " TODO 5、接口调用日志记录
        WHEN p_mod2.
          lt_taks =  fs_interface->get_tasks( iv_token = gv_token  iv_approval_code = approval_code_02 ).
          LOOP AT lt_taks ASSIGNING <fs>.
            CLEAR lt_data1.
            lt_data1 = fs_interface->get_instance( iv_token = gv_token iv_instance = <fs>-code ).
          ENDLOOP.
        WHEN p_mod3.
          lt_taks =  fs_interface->get_tasks( iv_token = gv_token  iv_approval_code = approval_code_03 ).
          LOOP AT lt_taks ASSIGNING <fs>.
            CLEAR lt_data1.
            lt_data1 = fs_interface->get_instance( iv_token = gv_token iv_instance = <fs>-code ).
          ENDLOOP.
        WHEN p_mod4.
          lt_taks =  fs_interface->get_tasks( iv_token = gv_token  iv_approval_code = approval_code_04 ).
          LOOP AT lt_taks ASSIGNING <fs>.
            CLEAR lt_data1.
            lt_data1 = fs_interface->get_instance( iv_token = gv_token iv_instance = <fs>-code ).
          ENDLOOP.
        WHEN p_mod5.
          lt_taks =  fs_interface->get_tasks( iv_token = gv_token  iv_approval_code = approval_code_05 ).
          LOOP AT lt_taks ASSIGNING <fs>.
            CLEAR lt_data1.
            lt_data1 = fs_interface->get_instance( iv_token = gv_token iv_instance = <fs>-code ).
          ENDLOOP.
        WHEN p_mod6.
          lt_taks =  fs_interface->get_tasks( iv_token = gv_token  iv_approval_code = approval_code_06 ).
          LOOP AT lt_taks ASSIGNING <fs>.
            CLEAR lt_data1.
            lt_data1 = fs_interface->get_instance( iv_token = gv_token iv_instance = <fs>-code ).
          ENDLOOP.
      ENDCASE.
    ENDIF.
  ENDMETHOD.
ENDCLASS.
